package com.dly2024.soft.service.serviceImpl;

import com.dly2024.soft.dto.UserDTO;
import com.dly2024.soft.mapper.TestTableMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class TestTableServiceImpl {

    @Resource
    private TestTableMapper testTableMapper;

    /**
     * 查询分页处理
     * @param page        需要查询第几页
     * @param limit       每页显示多少条
     * @param username    模糊查询的用户名称
     * @return            返回用户分页数据列表
     */
    public Map<String, Object> selectUsers(int page, int limit, String username, String password) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", testTableMapper.selectCount(username, password));    // 动态统计条数

        int index = (page - 1) * limit;
        map.put("data", testTableMapper.selectUsers(index, limit, username, password));
        return map;
    }

//    public Map<String, Object> insert(String username, String password) {
//        Map<String, Object> map = new HashMap<>();
//        map.put("code", 200);
//        map.put("msg", "新增成功");
//        testTableMapper.insert(username, password);
//        return map;
//    }

    public Map<String, Object> insert(UserDTO userDTO) {
        Map<String, Object> map = new HashMap<>();
        map.put("code", 200);
        map.put("msg", "新增成功");
        testTableMapper.insert(userDTO.getUsername(), userDTO.getPassword());
        return map;
    }
}
